<?php
// @formatter:off
/**
 * @file c.rule.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-16 23:56:49 +0000 (Sun, 16 Jun 2013) $
 *
 * $Id: c.rule.php 73 2013-06-16 23:56:49Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/themes/default/controlers/contents/c.rule.php $
 */
// @formatter:on

if($wcProfile->allowedTo(WC_PERM_RULE_EDIT)) {
	require_once "{$this->_themeRoot}/controlers/generics/bundlepreview.php";

	global $wcRule;
	global $wcRuleHooks;

	if($wcRule) {
		$wcThemeAssigns["PATH"] = "Rule '{$wcRule->name}'";
		$wcThemeAssigns["ACTIONCONTENT"] = "/contents/c.rule.html";
		$wcThemeAssigns['ACTIONPRESCRIPT'] = "/contents/c.rule.js";

		$list = array();
		foreach($wcRule->actions() as $action) {
			$aux = array(
				"id" => $action->id(),
				"name" => $action->name(),
				"action" => $wcRuleHooks[$action->type()]["edit"]
			);

			$list[] = $aux;
		}
		//
		// Creating multi-array by prefix for all rule actions.
		$lists = array();
		$ids = 1;
		$actions = $wcRule->actions();
		uasort($actions, "WCRuleAction::NameSort");
		foreach($actions as $action) {
			$name = $action->name();
			$prefix = strtoupper(isset($name[0]) ? $name[0] : "#");
			if($prefix < 'A' || $prefix > 'Z') {
				$prefix = '#';
			}
			$prefix = "{$prefix}";
			if(!isset($lists[$prefix])) {
				$lists[$prefix] = array(
					"prefix" => $prefix,
					"list" => array()
				);
			}

			$lists[$prefix]["list"][] = wcGetRuleActionPreviewAssigns($action);
		}
		ksort($lists);
		$wcThemeAssigns["ACTIONS-LISTS"] = $lists;
		//
		// Creating a list of action that can be created.
		$list = array();
		foreach($wcRuleHooks as $type => $hook) {
			$list[] = array(
				"type" => $type,
				"name" => $hook["name"],
				"creator" => $wcRuleHooks[$type]["create"]
			);
		}
		$wcThemeAssigns["ACTIONS-TYPES"] = $list;

		$wcThemeAssigns["RULE-COUNT"] = $wcRule->count;
		$wcThemeAssigns["actions"] = $list;

		if($wcProfile->allowedTo(WC_PERM_RULE_RENAME)) {
			$wcThemeAssigns["INFO-NAME-EDITABLE"] = true;
		}
	} else {
		$wcThemeAssigns["error-string"] = "Invalid rule";
		require "{$this->_themeRoot}/controlers/contents/c.500.php";
	}
} else {
	$wcThemeAssigns["error-string"] = "You're not allowed to open rules";
	require "{$this->_themeRoot}/controlers/contents/c.403.php";
}
?>